package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.base.EditableListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.PrinterSetting;

public class PrinterSetupAdapter extends MQBaseAdapter {

	public static final String PRINT_EDITED = "PrintEdit";
	private LayoutInflater mInflater;
	private List<PrinterSetting> listPrinter;
	private EditableListener mEditableListener;
	private Context shareContext;

	public PrinterSetupAdapter(Context context, List<PrinterSetting> listPrinter) {
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listPrinter = listPrinter;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<PrinterSetting> listPrinter) {
		this.listPrinter = listPrinter;
	}

	public int getCount() {
		return listPrinter.size();
	}

	public Object getItem(int position) {
		return listPrinter.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_print_row, null);
			holder.tv_printno = (TextView) convertView.findViewById(R.id.tv_printno);
			holder.tv_printname = (TextView) convertView.findViewById(R.id.tv_printname);
			holder.tv_printip = (TextView) convertView.findViewById(R.id.tv_printip);
			holder.tv_printport = (TextView) convertView.findViewById(R.id.tv_printport);
			holder.llPrintRow = (LinearLayout) convertView.findViewById(R.id.llPrintRow);

			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		
		holder.tv_printno.setText(String.valueOf(position + 1));
		listPrinter.get(position).setNo(position + 1);
		
		holder.tv_printname.setText(listPrinter.get(position).getPs_name());
		holder.tv_printip.setText(listPrinter.get(position).getPs_ip());
		if (listPrinter.get(position).getPs_port() == 0) {
			holder.tv_printport.setText("");
		}else {
			holder.tv_printport.setText(String.valueOf(listPrinter.get(position).getPs_port()));
		}

		if (position == selectedRow) {
			holder.llPrintRow.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.llPrintRow.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	@Override
	public void onTextChange(int resId, String changedText)  {
		
//		switch (resId) {
//		case R.id.et_bottledesc:
//			listBottle.get(selectedRow).setBo_desc(changedText);
//			break;
//		case R.id.et_bottleamt:
//			if (MoleQUtil.isNumeric2(changedText)) {
//				listBottle.get(selectedRow).setBo_amt(Double.valueOf(changedText));
//			}else{
//				listBottle.get(selectedRow).setBo_amt(0);
//			}
//			break;
//		}
//		listBottle.get(selectedRow).setEdit(true);
//		mEditableListener.onEdited(BOTTLE_EDITED);
	}

	public final class ViewHolder {
		public TextView tv_printno;
		public TextView tv_printname;
		public TextView tv_printip;
		public TextView tv_printport;
		private LinearLayout llPrintRow;
	}
}
